import axios from 'axios';
import {AUTH_USER, UNAUTH_USER, AUTH_ERROR} from '../actions/types'

export function loginProcessing({userName, password, remember}) {
    const URL = 'http://admin.yueduday.com/index.php?r=admin%2Flogin'
    return function (dispatch) {
        console.log(userName, password);
        dispatch(loginIn({username:'admin'}))
        localStorage.setItem('token', JSON.stringify({username:'admin'}));
        return false;
        // let config = {
        //     headers : {
        //         'Content-Type':'application/json;charset=UTF-8'
        //     },
        // };
        let userData = "username="+userName+ '&password='+password + '&remember=' + Boolean(remember);
        axios.post(URL, userData
        ).then(function(response) {
            if (response.data.code === 200) {
                const info = {name: response.data.username, 'token': response.data.auth_key};
                dispatch(loginIn(response.data.info))
                localStorage.setItem('info', JSON.stringify(response.data.info));
            } else {
                console.log(response.data);
                dispatch(authError('登陆失败，请重新登陆'))
            }
        }).catch(function (response) {
            console.log(response);
            dispatch(authError('登陆失败，请重新登陆'))
        });
    }
}
export function loginIn(info) {
    return {
      type: AUTH_USER,
      payload: info
    }
}
export function authError(error) {
    return {
      type: AUTH_ERROR,
      payload: error
    }
}
export function signoutUser() {
    localStorage.removeItem('token')
    return {
      type: UNAUTH_USER
    }
}



